//
//  SYDMineViewController.m
//  PinGJByOwnerSide
//
//  Created by lantaiyuan on 2017/8/8.
//  Copyright © 2017年 lantaiyuan. All rights reserved.
//

#import "SYDMineViewController.h"
#import "SYDMineCell.h"
#define mineID @"SYDMineCell"

@interface SYDMineViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *dataList;
@property(nonatomic,strong)personModel *model;

@property(nonatomic,strong)SYDAccount *account;
@end

@implementation SYDMineViewController
#pragma mark - 导航栏
- (void)navigationBarHandle{
    
    WEAKSELF
    [weakSelf backButtonAction:^{
        [weakSelf.navigationController popViewControllerAnimated:YES];

    }];
    
    [weakSelf rightNavButtonWithImage:[UIImage imageNamed:@"nav_button_set_up"] action:^{
        [self.navigationController pushViewController:[[SYDMySettingViewController alloc]init] animated:YES];

    }];
    [self.mineCenterView.userView jk_addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        SYDPersonalInfoViewController *personVc = [[SYDPersonalInfoViewController alloc]init];
        personVc.model = weakSelf.model;
        [self.navigationController pushViewController:personVc animated:YES];
    }];
}

-(void)getPersonInfoSWithServer{
    WEAKSELF
    [[SYDNetwork shareTool]getMineMaterialWithUserId:[YMSaveTool valueForKey:USER_ID] cityCode:[YMSaveTool valueForKey:CityCode] success:^(NSURLSessionDataTask *task, id responseObject) {
        @try {
            //NSLog(@"------responseObject:%@",responseObject);
            NSDictionary *jsonDict = responseObject[@"obj"];
            personModel *model = [personModel mj_objectWithKeyValues:jsonDict];
            
            weakSelf.model = model;
            
        } @catch (NSException *exception) {
            NSLog(@"exception-----:%@",exception.description);
        } @finally {
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error.description);
    }];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.account = [SYDAccountTool getAccount];
    [self getPersonInfoSWithServer];
    
    self.mineCenterView.userName = self.account.username == nil? @"侠名":self.account.username;
    self .mineCenterView.userIcon = self.account.headImageUrl;
    if ([self.account.accountStatus integerValue] == 1) {
        self.mineCenterView.userStatus = @"审核中";
    }else if ([self.account.accountStatus integerValue] == 2){
        self.mineCenterView.userStatus = @"审核通过";
    }else if ([self.account.accountStatus integerValue] == 3){
        self.mineCenterView.userStatus = @"审核未过";
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self navigationBarHandle];
    self.dataList = [NSMutableArray arrayWithObjects:@"我的车辆",@"我的钱包",@"我的行程", nil];
    [self.tableView registerNib:[UINib nibWithNibName:mineID bundle:nil] forCellReuseIdentifier:mineID];
    [self.tableView reloadData];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataList.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SYDMineCell *cell = [tableView dequeueReusableCellWithIdentifier:mineID];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.titleLbl.text = self.dataList[indexPath.row];
    
    if (indexPath.row == 2) {
        cell.separatorInset = UIEdgeInsetsMake(0, SCREEN_HEIGHT, 0, 0);
    }
    return  cell;
    
}
-(void)getPhoto{
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 44;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0) {
        [self.navigationController pushViewController:[[SYDMyCarViewController alloc]init] animated:YES];
        
    }else if (indexPath.row == 1){
        [self.navigationController pushViewController:[[SYDMyWalletViewController alloc]init] animated:YES];
    }else if (indexPath.row == 2){
        [self.navigationController pushViewController:[[SYDTravelViewController alloc]init] animated:YES];
    }
}

#pragma mark---lazy
-(UITableView *)tableView{
    if (!_tableView) {
        if (iPhone8_X) {
            _tableView = [[UITableView alloc]initWithFrame:CGRectMake(15, 232, SCREEN_WIDTH -30, 132) style:UITableViewStylePlain];
        } else {
            _tableView = [[UITableView alloc]initWithFrame:CGRectMake(15, 210, SCREEN_WIDTH -30, 132) style:UITableViewStylePlain];
        }
        _tableView.separatorInset = UIEdgeInsetsMake(0, 15, 0, 15);
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        [_tableView drawBorderWithCornerRadius:5];
        _tableView.scrollEnabled = NO;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        [self.view addSubview:_tableView];
    }
    return  _tableView;
}

-(personModel *)model{
    if (!_model) {
        _model = [[personModel alloc]init];
    }
    return _model;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

